package com.hyl.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "DispatchServlet1",urlPatterns = {"/do/*"})
public class DispatchServlet1 extends HttpServlet {

	private Map<String,String> urlMap = new HashMap<>();

	@Override
	public void init() throws ServletException {
		super.init();
		urlMap.put("list", "com.hyl.servlet.impl.PersonListServlet");
		urlMap.put("add", "com.hyl.servlet.impl.PersonAddServlet");
		urlMap.put("del", "com.hyl.servlet.impl.PersonDelServlet");
		urlMap.put("check", "com.hyl.servlet.impl.CheckNameServlet");

		urlMap.put("person", "com.hyl.servlet.PersonServlet");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("前端控制器");
		//获取请求的路径
		String uri  = request.getRequestURI();
		String path = request.getContextPath();
		System.out.println(uri+"\t "+path);

		//获取请求的对象
		String tmp = uri.substring(path.length()+4);
		///test/PersonServlet/list	 /test 	list
		System.out.println(uri+"\t "+path+" \t"+tmp);

		String className= urlMap.get(tmp);
		System.out.println(className); //com.hyl.servlet.impl.PersonListServlet

		try {
			//com.hyl.servlet.impl.PersonListServlet
			Class clz = Class.forName(className);
			//执行方法
			BaseServlet baseServlet = (BaseServlet)clz.newInstance();

			//执行前==》设置字符编码
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=UTF-8");
			request.setCharacterEncoding("utf-8");
			//执行
			//String ret = baseServlet.execute(request, response);
			//执行后
			//afterExecute(ret,request,response);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// dispatcher:index.jsp
	private void afterExecute(String result,HttpServletRequest request, HttpServletResponse response) throws Exception {
		/*
		* 转发=> dispatcher:index.jsp
		* 重定向=> redirect:list
		* ajax=> out:ok
		* */
		String path = result.substring(result.indexOf(':')+1);
		if(result.startsWith("dispatcher")){
			request.getRequestDispatcher(path).forward(request,response);//转发
		}else if(result.startsWith("redirect")){
			response.sendRedirect(path); //重定向
		}else if(result.startsWith("out")) {
			String msg = result.substring(result.indexOf(':')+1);
			PrintWriter out = response.getWriter();
			out.print(msg);
			out.close();
		}else{
			throw new Exception("响应的结果异常");
		}

	}
}
